<!DOCTYPE html>
<html>
<head>
<title>Network Configuration</title>
<meta name="description" content="A simple network configuration editor." />
<!-- Copyright 1998-2016 by Northwoods Software Corporation. -->
<meta charset="UTF-8">
<script src="go.js"></script>
<link href="../assets/css/goSamples.css" rel="stylesheet" type="text/css" />  <!-- you don't need to use this -->
<script src="goSamples.js"></script>  <!-- this is only for the GoJS Samples framework -->
<script id="code">
  function init() {
    if (window.goSamples) goSamples();  // init for these samples -- you don't need to call this
    var $ = go.GraphObject.make;

    myDiagram =
      $(go.Diagram, "myDiagramDiv",
        {
          allowDrop: true,
          initialContentAlignment: go.Spot.Center,
          "linkingTool.direction": go.LinkingTool.ForwardsOnly,
          "undoManager.isEnabled": true
        });

    // when the document is modified, add a "*" to the title and enable the "Save" button
    myDiagram.addDiagramListener("Modified", function(e) {
      var button = document.getElementById("saveModel");
      if (button) button.disabled = !myDiagram.isModified;
      var idx = document.title.indexOf("*");
      if (myDiagram.isModified) {
        if (idx < 0) document.title += "*";
      } else {
        if (idx >= 0) document.title = document.title.substr(0, idx); 
      }
    });

    myDiagram.nodeTemplateMap.add("Generator",
      $(go.Node,
        { locationSpot: go.Spot.Center, background: "darkblue" },
        new go.Binding("location", "location", go.Point.parse).makeTwoWay(go.Point.stringify),
        $(go.Shape, "ACVoltageSource",
          { stroke: "white", strokeWidth: 3, fill: "transparent",
            width: 40, height: 40, margin: 5,
            portId: "", fromLinkable: true, cursor: "pointer", fromSpot: go.Spot.TopBottomSides, toSpot: go.Spot.TopBottomSides
          })
      ));

    myDiagram.nodeTemplateMap.add("Connector",
      $(go.Node,
        { locationSpot: go.Spot.Center, background: "gray" },
        new go.Binding("location", "location", go.Point.parse).makeTwoWay(go.Point.stringify),
        $(go.Shape, "Circle",
          { stroke: null, fill: $(go.Brush, "Linear", { 0: "lightgray", 1: "gray" }),
            width: 20, height: 20, margin: 2,
            portId: "", fromLinkable: true, cursor: "pointer", fromSpot: go.Spot.TopBottomSides, toSpot: go.Spot.TopBottomSides
          })
      ));

    myDiagram.nodeTemplateMap.add("Consumer",
      $(go.Node, "Spot",
        { locationSpot: go.Spot.Center, locationObjectName: "BODY",
          selectionObjectName: "BODY" },
        new go.Binding("location", "location", go.Point.parse).makeTwoWay(go.Point.stringify),
        $(go.Picture, "images/pc.jpg",
          { name: "BODY", width: 50, height: 40, margin: 2,
          portId: "", fromLinkable: true, cursor: "pointer", fromSpot: go.Spot.TopBottomSides, toSpot: go.Spot.TopBottomSides
          }),
        $(go.TextBlock,
          { alignment: go.Spot.Right, alignmentFocus: go.Spot.Left },
          new go.Binding("text", "key"))
      ));

    myDiagram.nodeTemplateMap.add("HBar",
      $(go.Node,
        new go.Binding("location", "location", go.Point.parse).makeTwoWay(go.Point.stringify),
        { layerName: "Background",
          // special resizing: just at the ends
          resizable: true, resizeObjectName: "SHAPE",
          resizeAdornmentTemplate:
            $(go.Adornment, "Spot",
              $(go.Placeholder),
              $(go.Shape,  // left resize handle
                { alignment: go.Spot.Left, cursor: "col-resize",
                desiredSize: new go.Size(6, 6), fill: "lightblue", stroke: "dodgerblue" }),
              $(go.Shape,  // right resize handle
                { alignment: go.Spot.Right, cursor: "col-resize",
                desiredSize: new go.Size(6, 6), fill: "lightblue", stroke: "dodgerblue" }))
            },
        $(go.Shape, "Rectangle",
          { name: "SHAPE",
            fill: "black", stroke: null, strokeWidth: 0,
            width: 1000, height: 4, 
            minSize: new go.Size(100, 4),
            maxSize: new go.Size(Infinity, 4) },
          new go.Binding("desiredSize", "size", go.Size.parse).makeTwoWay(go.Size.stringify),
          new go.Binding("fill"),
          { portId: "", toLinkable: true })
      ));

    myDiagram.linkTemplate =
      $(BarLink,  // subclass defined below
        {
          routing: go.Link.Orthogonal,
          relinkableFrom: true, relinkableTo: true,
          toPortChanged: function(link, oldport, newport) {
            if (newport instanceof go.Shape) link.path.stroke = newport.fill;
          }
        },
        $(go.Shape,
          { strokeWidth: 2 })
      );

    // start off with a simple diagram
    load();

    // initialize Palette
    myPalette =
      $(go.Palette, "myPaletteDiv",
        { nodeTemplateMap: myDiagram.nodeTemplateMap,
          layout:
            $(go.GridLayout,
              { cellSize: new go.Size(2, 2),
                isViewportSized: true })
        }
      );

    myPalette.model.nodeDataArray = [
      { "key": "Gen", "category": "Generator" },
      { "key": "Cons", "category": "Consumer" },
      { "key": "Dest", "category": "Connector" },
      { "key": "Bar", "category":"HBar", "size":"100 4"},
    ];

    // remove cursors on all ports in the Palette
    // make TextBlocks invisible, to reduce size of Nodes
    myPalette.nodes.each(function(node) {
        node.ports.each(function(port) {
            port.cursor = "";
          });
        node.elements.each(function(tb) {
            if (tb instanceof go.TextBlock) tb.visible = false;
          });
      });

    // initialize Overview
    myOverview =
      $(go.Overview, "myOverviewDiv",
        { observed: myDiagram,
          contentAlignment: go.Spot.Center });
  }


  function BarLink() {
    go.Link.call(this);
  }
  go.Diagram.inherit(BarLink, go.Link);

  /** @override */
  BarLink.prototype.getLinkPoint = function(node, port, spot, from, ortho, othernode, otherport) {
    if (node.category === "HBar") {
      var op = go.Link.prototype.getLinkPoint.call(this, othernode, otherport, this.computeSpot(!from), !from, ortho, node, port);
      var r = new go.Rect(port.getDocumentPoint(go.Spot.TopLeft),
                          port.getDocumentPoint(go.Spot.BottomRight));
      var y = (op.y > r.centerY) ? r.bottom : r.top;
      if (op.x < r.left) return new go.Point(r.left, y);
      if (op.x > r.right) return new go.Point(r.right, y);
      return new go.Point(op.x, y);
    } else {
      return go.Link.prototype.getLinkPoint.call(this, node, port, spot, from, ortho, othernode, otherport);
    }
  };

  /** @override */
  BarLink.prototype.getLinkDirection = function(node, port, linkpoint, spot, from, ortho, othernode, otherport) {
    var p = port.getDocumentPoint(go.Spot.Center);
    var op = otherport.getDocumentPoint(go.Spot.Center);
    var below = op.y > p.y;
    return below ? 90 : 270;
  };
  // end BarLink class


  // save a model to and load a model from JSON text, displayed below the Diagram
  function save() {
    document.getElementById("mySavedModel").value = myDiagram.model.toJson();
    myDiagram.isModified = false;
  }
  function load() {
    myDiagram.model = go.Model.fromJson(document.getElementById("mySavedModel").value);
  }
</script>
</head>
<body onload="init()">
<div id="sample">
  <div style="width:100%; white-space:nowrap;">
    <span style="display: inline-block; vertical-align: top; padding: 2px; width:140px">
      <div id="myPaletteDiv" style="background-color: lightgray; border: solid 1px black; height: 500px"></div>
      <div id="myOverviewDiv" style="border: solid 1px black; height: 100px"></div>
    </span>
    <span style="display: inline-block; vertical-align: top; padding: 2px; width:80%">
      <div id="myDiagramDiv" style="border: solid 1px black; height: 600px"></div>
    </span>
  </div>
  <div id="buttons">
    <button id="loadModel" onclick="load()">Load</button>
    <button id="saveModel" onclick="save()">Save</button>
  </div>
  <textarea id="mySavedModel" style="width:100%;height:300px">
{ "class": "go.GraphLinksModel",
  "nodeDataArray": [ 
{"key":"Gen1", "category":"Generator", "location":"300 0"},
{"key":"1", "category":"HBar", "location":"100 100", "size":"500 4", "fill":"green"},
{"key":"Cons1", "category":"Consumer", "location":"53 234"},
{"key":"2", "category":"HBar", "location":"0 300", "size":"600 4", "fill":"orange"},
{"key":"Conn1", "category":"Connector", "location":"232.5 207.75"},
{"key":"Cons3", "category":"Consumer", "location":"357.5 230.75"},
{"key":"Cons2", "category":"Consumer", "location":"484.5 164.75"}
 ],
  "linkDataArray": [ 
{"from":"Gen1", "to":"1"},
{"from":"Gen1", "to":"2"},
{"from":"Cons1", "to":"2"},
{"from":"Conn1", "to":"1"},
{"from":"Conn1", "to":"2"},
{"from":"Cons3", "to":"2"},
{"from":"Cons2", "to":"1"}
 ]}
  </textarea>
</div>
</body>

</html>
